package com.carpedil.shirodemo.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.carpedil.shirodemo.system.model.Dept;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Mapper 接口
 *
 * @author carpedil
 * @since 2021-01-05
 */
@Repository
public interface DeptMapper extends BaseMapper<Dept> {
  List<Dept> selectAllDeptTree();

  /**
   * selectDeptById
   *
   * @param parentId parentId
   * @return List<Dept>
   */
  @Select("select dept_id,dept_name,create_time " + " from dept" + " where parent_id = #{parentId}")
  List<Dept> selectDeptById(@Param("parentId") Integer parentId);

  @Update(
      "update dept as prev "
          + " join dept as next "
          + " on (prev.`dept_id` = #{currentId} and next.`dept_id` = #{swapId}) "
          + " or (prev.`dept_id` = #{swapId} and next.`dept_id` = #{currentId}) "
          + " set prev.`order_num` = next.`order_num`, next.`order_num` = prev.`order_num` ")
  void swap(@Param("currentId") Integer currentId, @Param("swapId") Integer swapId);

  @Select("select ifnull(max(order_num),0) from dept ")
  Integer selectMaxOrderNum();
}
